package worst.shopping.products;
import database.DataBaseKeys;
import database.ProductDAO;
import worst.shopping.R;
import worst.shopping.adapter.ImageCursorAdapter;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;


public class ManageProductsActivity extends ListActivity {
	private Cursor _cursor;
	private ProductDAO _productDAO;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manageproductsactivity_layout);
		
		_productDAO = new ProductDAO(getApplication());
		
		_cursor = _productDAO.getAllProductsCursor();
		startManagingCursor(_cursor);

		ListAdapter listAdapter = new ImageCursorAdapter(this, R.layout.listitem, _cursor, new String[] { DataBaseKeys.PRODUCT_NAME, DataBaseKeys.PRODUCT_ICON }, new int[]{ R.id.list_item_name, R.id.list_item_image});
		
		setListAdapter(listAdapter);
		
		registerForContextMenu(this.getListView());
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
	
		_cursor.moveToPosition(position);
		
		startProductViewFor(_cursor.getString(_cursor.getColumnIndex(DataBaseKeys.PRODUCT_NAME)));
	}
	
	private void startProductViewFor(String string) {
		Intent intent = new Intent(this, EditProductActivity.class);
		intent.putExtra(DataBaseKeys.PRODUCT_NAME, string);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.manageproductsactivity_optionsmenu, menu);
		return true;
	}
	
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		
		menu.add("Delete");
		menu.add("Edit");
		menu.add("Cancel");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	
		if(item.getTitle().toString().equals("Delete"))
		{
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			  			
			_cursor.moveToPosition(info.position);
			
			_productDAO.deleteProduct(_cursor.getString(_cursor.getColumnIndex(DataBaseKeys.PRODUCT_NAME)));
			_cursor.requery();
		}
		else if(item.getTitle().toString().equals("Edit"))
		{
			startProductViewFor(_cursor.getString(_cursor.getColumnIndex(DataBaseKeys.PRODUCT_NAME)));
		}
		
		return super.onContextItemSelected(item);
	}
	
	public void newProductButtonClicked(View view)
	{
		startProductViewFor("");
	}
	
	public void backButtonClicked(View view)
	{
		finish();
	}
}
